package Main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import Component.MyCheckBox;
import Component.MyComboBox;
import Component.MyForm;
import Component.MyLabel;
import Component.MyPanel;
import Component.MyTextBox;
import Component.MyTextButton;
import Component.MyTextCheckBox;
import Global.GlobalGameOption;
import Global.ResourceManager;
import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Huy
 */
public class GameOptionState extends GameState {

	final int PANEL_START_Y = -450;
	MyPanel negativePannel;
	MyPanel mainPannel;
	MyPanel panelGameSound;
	MyPanel panelGameScreen;
	MyPanel panelGamePlay;
	MyTextCheckBox checkEnableSoundEffect;
	MyTextCheckBox checkEnableMusic;
	MyComboBox comboboxResolution;
	MyComboBox comboboxTheme;
	MyComboBox comboboxGameSpeed;
	MyTextButton buttonGameSound;
	MyTextButton buttonGameScreen;
	MyTextButton buttonGamePlay;
	MyTextBox tb_Name;
	MyCheckBox checkFullScreen;
	static String[] files;
	private Color panelColor;
	MyPanel lastMovingPanel;
	MyPanel newMovingPanel;

	public GameOptionState(Game _game) {
		super(_game);
		panelColor = new Color(0xcc220066, true);
		form = new MyForm(ResourceManager.getImage("menu/background"), game.getWidth(), game.getHeight());
		mainPannel = new MyPanel(210, 190, panelColor, -200, 50);
		negativePannel = new MyPanel(180, 100, panelColor, -200, 400);

		panelGameSound = new MyPanel(400, 450, panelColor, 300, PANEL_START_Y);
		panelGameScreen = new MyPanel(400, 450, panelColor, 300, PANEL_START_Y);
		panelGamePlay = new MyPanel(400, 450, panelColor, 300, PANEL_START_Y);
		newMovingPanel = panelGamePlay;

		form.add(mainPannel);
		form.add(panelGameSound);
		form.add(panelGamePlay);
		form.add(panelGameScreen);
		form.add(negativePannel);

		MyTextButton button = new MyTextButton("Ok", 50, 60);
		MyTextButton defaultButton = new MyTextButton("Default", 50, 20);
		negativePannel.add(button);
		negativePannel.add(defaultButton);

		BufferedImage tabImage = ResourceManager.getImage("menu/optiontab");
		buttonGamePlay = new MyTextButton(tabImage, "Game Play", 10, 20);
		buttonGamePlay.setEnable(false);
		buttonGamePlay.setForeColor(Color.WHITE);
		buttonGamePlay.setDisableColor(Color.YELLOW);
		mainPannel.add(buttonGamePlay);

		buttonGameSound = new MyTextButton(tabImage, "Sound", 10, 70);
		buttonGameSound.setForeColor(Color.WHITE);
		buttonGameSound.setDisableColor(Color.YELLOW);
		mainPannel.add(buttonGameSound);

		buttonGameScreen = new MyTextButton(tabImage, "Display", 10, 120);
		buttonGameScreen.setForeColor(Color.WHITE);
		buttonGameScreen.setDisableColor(Color.YELLOW);
		mainPannel.add(buttonGameScreen);

		checkEnableSoundEffect = new MyTextCheckBox("Sound Effect", 50, 50);
		panelGameSound.add(checkEnableSoundEffect);

		checkEnableMusic = new MyTextCheckBox("Music", 50, 100);
		panelGameSound.add(checkEnableMusic);


		MyLabel labelSoundOption = new MyLabel("Sound Option", 150, 0);
		labelSoundOption.setForeColor(Color.WHITE);
		panelGameSound.add(labelSoundOption);

		MyLabel labelScreenOption = new MyLabel("Display Option", 150, 0);
		labelScreenOption.setForeColor(Color.WHITE);
		panelGameScreen.add(labelScreenOption);

		MyLabel labelScreenResolution = new MyLabel("Window size", 50, 50);
		labelScreenResolution.setForeColor(Color.WHITE);
		panelGameScreen.add(labelScreenResolution);

		comboboxResolution = new MyComboBox(new String[]{"800x600", "1024x768"}, 150, 50);
		panelGameScreen.add(comboboxResolution);

		checkFullScreen = new MyTextCheckBox("Full screen", 50, 100);
		panelGameScreen.add(checkFullScreen);

		MyLabel labelScreenTheme = new MyLabel("Theme", 50, 150);
		labelScreenTheme.setForeColor(Color.WHITE);
		panelGameScreen.add(labelScreenTheme);

		String path = "TexturePackages";
		File folder = new File(path);
		File[] listOfFiles = folder.listFiles();
		files = new String[listOfFiles.length];
		String[] names = new String[listOfFiles.length];
		for (int i = 0; i < listOfFiles.length; i++) {

			files[i] = path + "/" + listOfFiles[i].getName();
			names[i] = listOfFiles[i].getName();
		}


		comboboxTheme = new MyComboBox(names, 150, 150);

		panelGameScreen.add(comboboxTheme);

		MyLabel labelGamePlayOption = new MyLabel("Game Play Option", 150, 0);
		labelGamePlayOption.setForeColor(Color.WHITE);

		panelGamePlay.add(labelGamePlayOption);

		MyLabel labelName = new MyLabel("Player Name", 50, 50);
		labelName.setForeColor(Color.WHITE);
		panelGamePlay.add(labelName);

		tb_Name = new MyTextBox("Player", 150, 50);
		panelGamePlay.add(tb_Name);

		MyLabel labelGameSpeed = new MyLabel("Game Speed", 50, 100);
		labelGameSpeed.setForeColor(Color.WHITE);
		panelGamePlay.add(labelGameSpeed);

		comboboxGameSpeed = new MyComboBox(new String[]{"slow", "normal", "fast"}, 150, 100);
		panelGamePlay.add(comboboxGameSpeed);

		button.setMouseAdapter(new MouseAdapter() { // add listenner
			@Override
			public void mouseReleased(MouseEvent e) {
				try {
					writeOption();
					GlobalGameOption.getOption().writeXML();
					state = 3;

					
					Global.ResourceManager.initResource(GlobalGameOption.getOption().texturePackage);
				} catch (Exception ex) {
					Logger.getLogger(GameOptionState.class.getName()).log(Level.SEVERE, null, ex);
				}
			}
		});
		defaultButton.setMouseAdapter(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				GlobalGameOption.getOption().setDefault();
				readOption();
			}
		});
		buttonGameSound.setMouseAdapter(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (panelGameSound.getY() < 0) {
					newMovingPanel = panelGameSound;
					state = 2;
				}

				buttonGameSound.setEnable(false);
				buttonGameScreen.setEnable(true);
				buttonGamePlay.setEnable(true);
			}
		});
		buttonGameScreen.setMouseAdapter(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (panelGameScreen.getY() < 0) {
					newMovingPanel = panelGameScreen;
					state = 2;
				}

				buttonGameSound.setEnable(true);
				buttonGameScreen.setEnable(false);
				buttonGamePlay.setEnable(true);
			}
		});
		buttonGamePlay.setMouseAdapter(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				if (panelGamePlay.getY() < 0) {
					newMovingPanel = panelGamePlay;
					state = 2;
				}
				buttonGameSound.setEnable(true);
				buttonGameScreen.setEnable(true);
				buttonGamePlay.setEnable(false);
			}
		});
		readOption();
	}
	int state = 0;
	boolean soundisMoving = false;
	boolean screenisMoving = false;
	boolean gameplayisMoving = false;

	@Override
	public void update(float t) {
		if (state == 0) {
			newMovingPanel.move(0, 100);
			if (mainPannel.getSprite().getPosition().x < 50) {
				mainPannel.move(50, 0);
				negativePannel.move(50, 0);
			}
			if (newMovingPanel.getSprite().getPosition().y >= 50) {
				lastMovingPanel = newMovingPanel;
				state = 1;
			}
		} else if (state == 2) {
			lastMovingPanel.move(0, 100);
			if (lastMovingPanel.getSprite().getPosition().y > form.getHeight()) {
				lastMovingPanel.move(0, -lastMovingPanel.getSprite().getPosition().y + PANEL_START_Y);
				lastMovingPanel = newMovingPanel;
				state = 0;
			}
		}
		if (state == 3) {
			if (lastMovingPanel.getSprite().getPosition().y > form.getHeight()) {
				GlobalGameOption.MUSIC.getMusic("menu").stop();
				GlobalGameOption.getOption().refresh();
				game.gameState = new GameMenuState(game);
				state = 0;
			}
			lastMovingPanel.move(0, 100);
			mainPannel.move(-50, 0);
			negativePannel.move(-50, 0);

		}
	}

	private int getFileIndex(String file) {
		//String file = GlobalGameOption.getOption().texturePackage;
		for (int i = 0; i < files.length; i++) {
			String string = files[i];
			if (string.equalsIgnoreCase(file)) {
				return i;
			}
		}
		return 0;
	}

	public void readOption() {
		tb_Name.setText(GlobalGameOption.getOption().playerName);
		checkFullScreen.setChecked(GlobalGameOption.getOption().isFullScreen);
		comboboxGameSpeed.setSelectedIndex(GlobalGameOption.getOption().speed);
		comboboxResolution.setSelectedIndex(GlobalGameOption.getOption().resolution);
		comboboxTheme.setSelectedIndex(getFileIndex(GlobalGameOption.getOption().texturePackage));

		checkEnableSoundEffect.setChecked(GlobalGameOption.getOption().isSound);
		checkEnableMusic.setChecked(GlobalGameOption.getOption().isMusic);
	}

	public void writeOption() {
		GlobalGameOption.getOption().playerName = tb_Name.getText();
		GlobalGameOption.getOption().isFullScreen = checkFullScreen.isChecked();
		GlobalGameOption.getOption().speed = comboboxGameSpeed.getSelectedIndex();
		GlobalGameOption.getOption().resolution = comboboxResolution.getSelectedIndex();
		GlobalGameOption.getOption().texturePackage = files[comboboxTheme.getSelectedIndex()];

		GlobalGameOption.getOption().isSound = checkEnableSoundEffect.isChecked();
		GlobalGameOption.getOption().isMusic = checkEnableMusic.isChecked();
		
	}
}
